package sceneInfo;

import java.util.ArrayList;
import java.util.Vector;
import java.io.Serializable;

import rcscene.ObjectWeights;

public class FuzzyVisualInfo implements Serializable {
	
public static final long serialVersionUID = 2L;

public static final int NUMBER_OF_OBJECTTYPES = 7;
public static final float DEF_OBJ_WEIGHT = 1.0f;
/**
 * names of object types
 */
public static final String OBJECTTYPENAME [] = {"Ball", "Teammates", "Oppos", "Unknown", "Goal", "Flags","Lines"};

//a fuzzy framework to go with all this.
FuzzyFramework framework;
//it's in the VisualInfo... number of scene
private int	m_time;

/** object weights : we use them to decide whether to compute the histograms : 
 * when the weight of an object type is 0, the histogram is not computed*/
private ObjectWeights objweights;

// a big histogram with all objects 
// dimension = 7 * dim (objectHistograms)
public int [] BigHistogram;
	
// Histograms for each category
 private ObjectHistogram ballHist;
 private ObjectHistogram teammateHist; 
 private ObjectHistogram opponentHist;
 private ObjectHistogram unknownPlayerHist;
 private ObjectHistogram goalHist;
 private ObjectHistogram flagHist;
 private ObjectHistogram lineHist;

 //Object weights
 
 
 /** constructor
  * takes a fuzzy framework and object weights
  */
 public FuzzyVisualInfo(FuzzyFramework frame){
	 
	 framework = frame;
	 ballHist = null;
	 teammateHist = null;
	 opponentHist = null;
	 unknownPlayerHist = null;
	 goalHist = null;
	 flagHist= null;
	 lineHist = null;
	 BigHistogram = null;
	 objweights= null;
	  
 }
 

 // getters for the histograms
 public ObjectHistogram getBallHist(){
	return ballHist; 
 }
 public ObjectHistogram getTeammateHist(){
		return teammateHist; 
	 }
 public ObjectHistogram getOpponentHist(){
		return opponentHist; 
	 }
 public ObjectHistogram getUnknownPlayerHist(){
		return unknownPlayerHist; 
	 }
 public ObjectHistogram getGoalHist(){
		return goalHist; 
	 }
 public ObjectHistogram getFlagHist(){
		return flagHist; 
	 }
 public ObjectHistogram getLineHist(){
		return lineHist; 
	 }


 
 public int[] getObjectCount(){
	 int[] res = new int[NUMBER_OF_OBJECTTYPES];
	 res[0] = ballHist.getSize(); 
	 res[1] = teammateHist.getSize();
	 res[2] = opponentHist.getSize();
	 res[3] = unknownPlayerHist.getSize();
	 res[4] = goalHist.getSize();
	 res[5] = flagHist.getSize();
	 res[6] = lineHist.getSize();

	 return res;
 }
 
 /** compute all the histograms representing the objects seen (from reference VisualInfo object)
  */
 public void computeHistograms(VisualInfo vinfo){
	 
	 if (objweights == null) {
		 //System.err.println("Warning : Object weights not set ! using default");
		 objweights = new ObjectWeights();
		 objweights.setBallWeight( DEF_OBJ_WEIGHT);
		 objweights.setTeamPlayerWeight( DEF_OBJ_WEIGHT);
		 objweights.setOpponentPlayerWeight( DEF_OBJ_WEIGHT);
		 objweights.setUnknownPlayerWeight( DEF_OBJ_WEIGHT);
		 objweights.setGoalWeight( DEF_OBJ_WEIGHT);
		 objweights.setFlagWeight( DEF_OBJ_WEIGHT);
		 objweights.setLinesWeight( DEF_OBJ_WEIGHT);
		
	 }
	 // create every histogram from the object list of the VisualInfo
	 // only compute histogram if object weight is not 0
	 if(objweights.getBallWeight() >0) 
		 ballHist = new ObjectHistogram("ball", vinfo.getBallList(), framework);
	 else 
		 ballHist = new ObjectHistogram(framework);

	 if(objweights.getTeamPlayerWeight() >0)
		 teammateHist = new ObjectHistogram("teammate", vinfo.getTeammatesList(),framework);
	 else 
		 teammateHist = new ObjectHistogram(framework);

	 if(objweights.getOpponentPlayerWeight() >0) 
		 opponentHist = new ObjectHistogram("opponent", vinfo.getOpponentsList(), framework);
	 else 
		 opponentHist = new ObjectHistogram(framework);

	 if(objweights.getUnknownPlayerWeight() >0) 
		 unknownPlayerHist = new ObjectHistogram("unknownplayer", vinfo.getUnknownPlayersList(), framework);
	 else 
		 unknownPlayerHist = new ObjectHistogram(framework);

	 if(objweights.getGoalWeight() >0) 
		 goalHist = new ObjectHistogram("goal", vinfo.getGoalList(), framework);
	 else 
		 goalHist = new ObjectHistogram(framework);

	 if(objweights.getFlagWeight() >0) 
		 flagHist= new ObjectHistogram("flag", vinfo.getFlagList(), framework);
	 else 
		 flagHist = new ObjectHistogram(framework);

	 if(objweights.getLinesWeight() >0) 
		 lineHist = new ObjectHistogram("line", vinfo.getLineList(), framework);
	 else
		 lineHist = new ObjectHistogram(framework);
	 
	 m_time = vinfo.getTime();
	 
 }

 
 //compute the big one from the small ones.
 public void computeBigHistogram(){
 
	 int dimensionOfSmall = framework.alphanumber*framework.distnumber;
	 //new array of appropriate size 
	 BigHistogram = new int [NUMBER_OF_OBJECTTYPES*dimensionOfSmall]; 	 
	 
	 for (int i =0; i <dimensionOfSmall; i++){
		 BigHistogram[i] = ballHist.getHistogram(i);
		 BigHistogram[i+ dimensionOfSmall] = teammateHist.getHistogram(i);
		 BigHistogram[i+ 2*dimensionOfSmall] = opponentHist.getHistogram(i);
		 BigHistogram[i+ 3*dimensionOfSmall] = unknownPlayerHist.getHistogram(i);
		 BigHistogram[i+ 4*dimensionOfSmall] = goalHist.getHistogram(i);
		 BigHistogram[i+ 5*dimensionOfSmall] = flagHist.getHistogram(i);
		 BigHistogram[i+ 6*dimensionOfSmall] = lineHist.getHistogram(i);
	 }
	 
 }

public boolean equals(FuzzyVisualInfo f2){
	
	int dimension = BigHistogram.length;
	 for (int i =0; i <dimension; i++){
		 if (BigHistogram[i] != f2.BigHistogram[i]){return false;}
	 }
	return true;
}

public void setObjectWeights(ObjectWeights ow){
	objweights = ow;
}

//////////////////////////////////////////////////
 public int getTime() {
	return m_time;
}

 /**
  * get a histogram by its object type
  * @param objectType
  * @return
  */
public ObjectHistogram getHistogram(int objectType) {
	//{"Ball", "Teammates", "Oppos", "Unknown", "Goal", "Flags","Lines"};
	switch(objectType){
	case 0:
		return getBallHist();
	case 1:
		return getTeammateHist();
	case 2:
		return getOpponentHist();
	case 3:
		return getUnknownPlayerHist();
	case 4:
		return getGoalHist();
	case 5:
		return getFlagHist();
	case 6:
		return getLineHist();
	default:
		 return null;
	}
}
 
}
